var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subjectArray: [
    ],
    sty: 'position: absolute;left: 480rpx;top: 193rpx;',
    address: '请选择机构的地址',
    titleStyle: 'font-size:15px; color:#434343;font-weight:normal;',
    imgStyle: 'width:28rpx; height:34rpx;',
    courseTitle: '',
    courseType: '',
    courseOldPrice: '',
    courseNewPrice: '',
    courseTeacher: '',
    courseIntroduce: '',
    isKnow:'',
    studentNumber:'',
    courseImg:'',
    date:''
  },
  getPeopleNumber:function(event){
    this.setData({
      studentNumber:event.detail.value
    })
  },
  getCourseName: function (e) {
    console.log(e)
    this.setData({
      courseTitle: e.detail.value
    })
    console.log(this.data.courseTitle)
  },
  getCourseType: function (e) {
    this.setData({
      courseType: e.detail.id
    })
  },
  getOldCoursePrice: function (e) {
    console.log(e)
    this.setData({
      courseOldPrice: e.detail.value
    })
  },
  
  getNewCoursePrice: function (e) {
    console.log(e)
    this.setData({
      courseNewPrice: e.detail.value
    })
    console.log(this.data.courseNewPrice)
  },
  // getCourseTeacher: function (e) {
  //   this.setData({
  //     courseTeacher: e.detail.value
  //   })
  // },
  getCourseIntroduce: function (e) {
    this.setData({
      courseIntroduce: e.detail.value
    })
  },
  getDate: function (e) {
    console.log(e)
    this.setData({
      date: e.detail.value
    })
    console.log(this.data.date)
  },
  uploadImg:function(e){
    wx.showLoading({
      title: '正在上传图片',
      mask:true
    })
    var that=this
    console.log(e)
    var fileArr = e.detail.path.tempFilePaths
    for(var i=0;i<fileArr.length;i++){
      var filePath = fileArr[i]
      wx.uploadFile({
        url: app.globalData.baseUrl +'file/wechat',
        header:{
          'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
        },
        formData:{
           'modular':4
        },  
        filePath: filePath,
        name: 'files',
        success:function(res){
          wx.hideLoading()
          console.log(res)
          console.log(typeof res.data)
          var imgData = JSON.parse(res.data)
          console.log(imgData)
          console.log(imgData.data)
          that.setData({
            courseImg: imgData.data
          })
        console.log(that.data.courseImg)
        }
      })
    }
     
  },
  isKnow:function(event){
     this.setData({
       isKnow:event.detail.k
     })
  },
  chooseAdd: function (event) {
    var that = this
    var location
    var add
    //这是一个异步函数
    wx.chooseLocation({
      success: function (res) {
        var add = res.address
        console.log(add.length)
        if (add.length > 20) {
          add = res.name
        }
        that.setData({
          address: add
        })

      },
    })

  },
  submit: function (e) {
    var that = this
    console.log('img')
    console.log(this.data.courseImg)
    console.log('new')
    console.log(that.data.courseNewPrice)
    console.log('old')
    console.log(that.data.courseOldPrice)
    console.log('title')
    console.log(that.data.courseTitle)
    console.log('introduce')
    console.log(that.data.courseIntroduce)
    console.log('date')
    console.log(that.data.date)
    if(this.data.isKnow)
    {
      wx.request({
        url: app.globalData.baseUrl + 'mechanism/course',
        method: 'POST',
        data: {
          title: that.data.courseTitle,
          type: 1,
          priceNow: that.data.courseNewPrice,
          priceOld: that.data.courseOldPrice,
          introduce: that.data.courseIntroduce,
          schoolTimeDescription: that.data.date,
          studentTotal:that.data.studentNumber,
          position: that.data.address,
          coverImg:that.data.courseImg

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
        },
        success: function (res) {
          console.log(res)
          if (res.data.msg = '请求成功') {
            wx.navigateBack({

            })
            wx.showToast({
              title: '发布成功',
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: res.data.msg,
          })
        }, 
      })
    }else{
      wx.showToast({
        title: '请先勾选免责声明',
        icon:'none',
        duration:2000,
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'demand/listcourse',
      method: 'get',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        var resData = res.data.data.data
        for (var i = 0; i < resData.length; i++) {
          var id = 'subjectArray[' + i + '].id'
          var value = 'subjectArray[' + i + '].value'
          that.setData({
            [id]: resData[i].id,
            [value]: resData[i].name
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})